local wanshouguiwei = fk.CreateSkill {
  name = "inker_wanshouguiwei",
  tags = { Skill.Compulsory }
}

local DIY = require "packages.diy_utility.diy_utility"

local function checkWanshouguiwei(room)
  local basics = 0
  for _, cid in ipairs(table.connectIfNeed(DIY.getCenterCards(room), room.processing_area)) do
    local card = Fk:getCardById(cid)
    if card.type == Card.TypeBasic then
      basics = basics + 1
      if basics == 2 then
        return true
      end
    end
  end
end

wanshouguiwei:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, "@@inker_wanshouguiwei", 0)
end)
wanshouguiwei:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  player.room:addSkill("#CenterArea")
  if checkWanshouguiwei(room) then
    room:setPlayerMark(player, "@@inker_wanshouguiwei", 1)
  end
end)

---@type TrigSkelSpec<fun(self: TriggerSkill, event: TriggerEvent, target: nil, player: ServerPlayer, data: any):any>
local spec = {
  anim_type = "offensive",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(wanshouguiwei.name) and player:getMark("@@inker_wanshouguiwei") ~= (checkWanshouguiwei(player.room) and 1 or 0)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("@@inker_wanshouguiwei") == 0 then
      room:notifySkillInvoked(player, wanshouguiwei.name, "offensive")
      player:broadcastSkillInvoke(wanshouguiwei.name, table.random({1,2}))
      room:setPlayerMark(player, "@@inker_wanshouguiwei", 1)
    else
      room:setPlayerMark(player, "@@inker_wanshouguiwei", 0)
    end
  end,
}

wanshouguiwei:addEffect(fk.AfterCardsMove, spec)
wanshouguiwei:addEffect(DIY.AfterCenterAreaRemove, spec)

local axe_cost = function(self, event, target, player, data)
  local room = player.room
  local cards = {}
  for _, id in ipairs(player:getCardIds("he")) do
    if not player:prohibitDiscard(id) then
      table.insert(cards, id)
    end
  end
  cards = room:askToDiscard(player, {
    min_num = 2,
    max_num = 2,
    include_equip = true,
    skill_name = "#axe_skill",
    cancelable = true,
    pattern = tostring(Exppattern{ id = cards }),
    prompt = "#axe-invoke-any:" .. data.from.id .. ":"..data.to.id, skip = true })
  if #cards > 0 then
    event:setCostData(self, {cards = cards})
    return true
  end
end

-- 蘸金desu
wanshouguiwei:addEffect(fk.CardEffectCancelledOut, {
  is_delay_effect = true,
  audio_index = {3, 4},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(wanshouguiwei.name) and player:getMark("@@inker_wanshouguiwei") ~= 0 and
      data.card.trueName == "slash" and not data.to.dead and
      Fk.skills["#axe_skill"]:isEffectable(player)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, wanshouguiwei.name, "offensive")
    local axe = Fk.skills["#axe_skill"] ---@cast axe TriggerSkill
    -- Fk.skills["#axe_skill"]:doCost(event, target, player, data)

    -- 覆写doCost以替换烧条提示
    room.current_cost_skill = axe
    local ret = axe_cost(axe, event, target, player, data) -- 执行消耗

    room.current_cost_skill = nil

    local cost_data_bak = event:getCostData(axe)
    room.logic:trigger(fk.BeforeTriggerSkillUse, player, { skill = axe, willUse = ret })
    --self.cost_data = cost_data_bak

    if ret then -- 如果完成了消耗，则执行技能效果，并判断是否要终结此时机
      local skill_data = {cost_data = cost_data_bak, tos = {}, cards = {}}
      if cost_data_bak and type(cost_data_bak) == "table" then
        skill_data.tos = cost_data_bak.tos
        skill_data.cards = cost_data_bak.cards
      end
      local skillEffectData = room:useSkill(player, axe, function()
        return axe:use(event, target, player, data)
      end, skill_data)
      return skillEffectData.trigger_break
    end
  end,
})
wanshouguiwei:addEffect("atkrange", {
  fixed_func = function (self, from)
    if from:hasSkill(wanshouguiwei.name) and from:getMark("@@inker_wanshouguiwei") ~= 0 then
      return 3
    end
  end,
})

Fk:loadTranslationTable{
  ["inker_wanshouguiwei"] = "万兽归位",
  [":inker_wanshouguiwei"] = "锁定技，若中央区和处理区内有共计至少两张基本牌，你视为装备着所有【杀】均能发动的【贯石斧】。",

  ["#axe-invoke-any"] = "贯石斧：你可以弃置两张牌，令 %src 对 %dest 使用的【杀】依然生效",

  ["@@inker_wanshouguiwei"] = "万兽归位",

  ["$inker_wanshouguiwei1"] = "哪怕这条路的终点只剩我一人，我也不会迟疑。",
  ["$inker_wanshouguiwei2"] = "荣耀背后的沉重，我已深有体会。",
  ["$inker_wanshouguiwei3"] = "他们心底里渴望从我这得到的东西，就是我要彻底砸烂的东西。",
  ["$inker_wanshouguiwei4"] = "人们往往只会记住传说中何人持剑，而无视剑被谁铸就。",
}

return wanshouguiwei